package demo1;

import java.util.Arrays;

class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student(String name) {
        this.name = name;
    }

    //@Override
   /* public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }*/

    @Override
    public int compareTo(Student o) {
        Student s = (Student) o;
        if (this.age > o.age) {
            return -1;
        } else if (this.age < o.age) {
            return 1;
        } else {
            return 0;


        }
    }
}

public class Test {
    public static void main(String[] args) {

        String[] str = {"hello","world","!"};
        System.out.println(Arrays.toString(str));//String类中重写了Object中的toString
        Student[] student = new Student[3];
        student[0] = new Student("wish",20);
        student[1] = new Student("you",19);
        student[2] = new Student("happy",18);
        Arrays.sort(student);
        System.out.println(Arrays.toString(student));//需要重写Object中的toString方法，
        // 因为调用Arrays里的toString时，还是会用到Object里的toString的，
        //https://blog.csdn.net/weixin_61061381/article/details/124250415
        System.out.println(student);//输出的是地址
        System.out.println(student[0]);//输出的是值//会调用子类重写的toString

        //若没重写Object中的toString方法
    }
}
